package com.source.hotelbath.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.source.hotelbath.FragmentFactory;
import com.source.hotelbath.R;
import com.source.hotelbath.activity.CreateOrderActivity;
import com.source.hotelbath.activity.HotelSelectAct;
import com.source.hotelbath.base.BaseFragment;
import com.source.hotelbath.model.HodelBean;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.net.response.HotelListResponse;
import com.source.hotelbath.popdialog.HotelChooseListener;
import com.source.hotelbath.utils.SharedPreferencesUtils;

import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/23.
 * 订单的界面
 */

public class OrderFragment extends BaseFragment {

    public static final String TAG = OrderFragment.class.getSimpleName();
    private LinearLayout ll_header_right;
    private Button tv_undo;
    private Button tv_done;
    private HotelListResponse hotelListResponse;
    private MyHotelListener listener;
    private int location;
    private int hotelId;
    private String hotelName;
    private TextView tv_hotel;
    private ImageView iv_clear;
    private LinearLayout ll_hotel;


    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup var2, Bundle var3) {
        View view=layoutInflater.inflate(R.layout.fragment_order, null);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        LinearLayout ll_header_left= (LinearLayout) view.findViewById(R.id.ll_header_left);
        ll_header_left.setVisibility(View.GONE);
        ll_header_right = (LinearLayout) view.findViewById(R.id.ll_header_right);
        TextView tv_header_right = (TextView) view.findViewById(R.id.tv_header_right);
        tv_header_right.setText("筛选");
        TextView tv_header_title= (TextView) view.findViewById(R.id.tv_header_title);
        tv_header_title.setText("订单");
        tv_undo = (Button) view.findViewById(R.id.tv_undo);
        tv_done = (Button) view.findViewById(R.id.tv_done);
        tv_hotel = (TextView) view.findViewById(R.id.tv_hotel);
        iv_clear = (ImageView) view.findViewById(R.id.iv_clear);
        ll_hotel = (LinearLayout) view.findViewById(R.id.ll_hotel);
        ll_hotel.setVisibility(View.GONE);
        ll_header_right.setOnClickListener(this);
        tv_done.setOnClickListener(this);
        tv_undo.setOnClickListener(this);
        iv_clear.setOnClickListener(this);
        String hotelString=SharedPreferencesUtils.getStringData(getContext(),"hotelList","");
        if (!TextUtils.isEmpty(hotelString)){
            Gson gson = new Gson();
            hotelListResponse=gson.fromJson(hotelString,HotelListResponse.class);
        }
        onClick(tv_undo);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==ll_header_right){
            Intent intent=new Intent(getContext(),HotelSelectAct.class);
            intent.putExtra("TAG","OrderFragment");
            startActivityForResult(intent,1554);
        }else if(view==tv_done){
            changeState(view);
            showFragmentByTag(DoneFragment.TAG);
        }else if(view==tv_undo){
            changeState(view);
            showFragmentByTag(UndoneFragment.TAG);
        }else if(view==iv_clear){
            ll_hotel.setVisibility(View.GONE);
            iv_clear.setVisibility(View.GONE);
            tv_hotel.setText("");
            Intent intents = new Intent(CustomerRequestConstants.REFRESH_ORDER);
            intents.putExtra("hotelId",0);
            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intents);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==((Activity)getContext()).RESULT_OK){
            hotelId=data.getIntExtra("hotelId",0);
            hotelName = data.getStringExtra("hotelName");
            tv_hotel.setText(hotelName);
            iv_clear.setVisibility(View.VISIBLE);
            ll_hotel.setVisibility(View.VISIBLE);
            Intent intents = new Intent(CustomerRequestConstants.REFRESH_ORDER);
            intents.putExtra("hotelId",hotelId);
            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intents);
        }
    }

    class MyHotelListener extends HotelChooseListener {


        public MyHotelListener(Context context, List<HodelBean> hodelBeanList, int position) {
            super(context, hodelBeanList, position);
        }

        @Override
        public void getItem(String timeStr, int position) {

            hotelName = hotelListResponse.getHotelList().get(position).getHotelName();
            hotelId = hotelListResponse.getHotelList().get(position).getHotelId();
//            adapter.addData(hotelId);
            location=position;
            Intent intents = new Intent(CustomerRequestConstants.REFRESH_ORDER);
            intents.putExtra("hotelId",hotelId);
            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intents);
//            tv_hotel.setText(hotelName);
        }
    }

    private void changeState(View view) {
        tv_done.setSelected(false);
        tv_undo.setSelected(false);
        view.setSelected(true);

    }
    private void showFragmentByTag(String tag) {
        FragmentManager manager=getChildFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();

        List<Fragment> fragments = manager.getFragments();
        Fragment fragment = manager.findFragmentByTag(tag);

        if (null != fragments) {
            for (int i = 0; i < fragments.size(); i++) {
                if (null == fragments.get(i)  )
                    continue;
                ft.hide(fragments.get(i));
                Log.d("fragments.get(i)",i+"----");
            }
            if (null == fragment) {
                fragment = FragmentFactory.getInstance().getFragmentByTag(manager, tag);
                ft.add(R.id.fl_order, fragment, tag);
            }
            ft.show(fragment);
            ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
            getChildFragmentManager().executePendingTransactions();
        } else {
            if (null == fragment) {
                fragment = FragmentFactory.getInstance().getFragmentByTag(manager, tag);
                ft.add(R.id.fl_order, fragment, tag);
                ft.show(fragment);
                ft.addToBackStack(null);
                ft.commitAllowingStateLoss();
                manager.executePendingTransactions();
            }
//            Log.d(t, "fragments null!");
        }
    }
}
